package cn.darkfire.web.Utils;

import org.apache.commons.mail.SimpleEmail;

import java.security.SecureRandom;
import java.util.Random;

public class EmailUtils {
    private static final EmailUtils instance = new EmailUtils();

    public boolean isEmail(String email){
        return false;
    }

    public String sendEmail(String email){
        String code = "";
        code  = getCode();
        SimpleEmail sem = new SimpleEmail();
        sem.setHostName("smtp.qq.com");
        sem.setSSLOnConnect(true);
        sem.setSmtpPort(465);
        sem.setAuthentication("o0xdf@qq.com","lq*zp*****cjcaac");
        try{
            sem.setFrom("o0xdf@qq.com");//发件人邮箱
            sem.addTo(email);//收件人邮箱
            //邮件内容
            sem.setSubject("验证码登录");//标题
            sem.setMsg("您的验证码是:"+code);//正文
            //发送
            sem.send();
            return code;
        }catch (Exception e){
            return "-1";
        }

    }

    public static EmailUtils getInstance() {
        return instance;
    }

    public String getCode(){
        String code = "";
        char[] codes = "23456789zxcvbnmasdfghjklqwertyuop".toCharArray();
        Random rand = new SecureRandom(); //相比Random更加的安全,不会产生重复的伪随机数
        //开始生成验证码
        for(int i = 0;i<6;i++){
            code += codes[rand.nextInt(codes.length-1)];
        }
        return code;
    }
}
